function getCode(provider?: 'weixin' | 'qq' | 'sinaweibo' | 'xiaomi' | 'apple' | 'univerify'): Promise<string> {
  if (provider == null) {
    provider = 'weixin'
  }
  return new Promise((resolve, reject) => {
    uni.login({
      provider: provider,
      onlyAuthorize: true, // 微信登录仅请求授权认证
      success: function (event) {
        resolve(<string>event.code)
      },
      fail: (err) => {
        reject(null)
      }
    })
  })
}

function getEncryptedDataAndIv(provider?: 'weixin' | 'qq' | 'sinaweibo' | 'xiaomi' | 'apple'): Promise<{
  encryptedData: string
  iv: string
}> {
  return new Promise((resolve, reject) => {
    uni.getUserInfo({
      provider: provider ?? 'weixin',
      lang: 'zh_CN',
      success: ({ encryptedData, iv }) => {
        resolve({
          encryptedData,
          iv
        })
      },
      fail: (err) => {
        reject(null)
      }
    })
  })
}

export default function useGetLoginInfo() {
  return {
    getCode,
    getEncryptedDataAndIv
  }
}
